<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./Pulse_Generator.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Converts a change in `level_in` (an edge) into a pulse lasting one clock cycle. **The input edge must be synchronous to the clock.** The pulse outputs are combinational: a given pulse is generated in the same cycle as the relevant change in signal level.">
<title>Pulse Generator</title>
</head>
<body>

<p class="inline bordered"><b><a href="./Pulse_Generator.v">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>Pulse Generator</h1>
<p>Converts a change in <code>level_in</code> (an edge) into a pulse lasting one clock
 cycle. <strong>The input edge must be synchronous to the clock.</strong> The pulse
 outputs are combinational: a given pulse is generated in the same cycle as
 the relevant change in signal level.</p>
<p>A Pulse Generator can eliminate some simple FSMs by converting a condition
 of unknown length into a one-shot event (e.g.: updating a register only
 once when a signal changes for an unknown time). </p>

<pre>
`default_nettype none

module <a href="./Pulse_Generator.html">Pulse_Generator</a>
(
    input   wire    clock,
    input   wire    level_in,
    output  reg     pulse_posedge_out,
    output  reg     pulse_negedge_out,
    output  reg     pulse_anyedge_out
);

    initial begin
        pulse_posedge_out = 1'b0;
        pulse_negedge_out = 1'b0;
        pulse_anyedge_out = 1'b0;
    end
</pre>

<p>Create a version of the input delayed by one cycle. </p>

<pre>
    wire level_in_delayed;

    <a href="./Register.html">Register</a>
    #(
        .WORD_WIDTH     (1),
        .RESET_VALUE    (1'b0)
    )
    delay
    (
        .clock          (clock),
        .clock_enable   (1'b1),
        .clear          (1'b0),
        .data_in        (level_in),
        .data_out       (level_in_delayed)
    );
</pre>

<p>When the input changes before its delayed version, immediately raise the
 relevant output.  On the next clock cycle, the delayed version will arrive
 and the raised output will go back low.</p>

<pre>
    always @(*) begin
        pulse_posedge_out = (level_in          == 1'b1) && (level_in_delayed  == 1'b0);
        pulse_negedge_out = (level_in          == 1'b0) && (level_in_delayed  == 1'b1);
        pulse_anyedge_out = (pulse_posedge_out == 1'b1) || (pulse_negedge_out == 1'b1);
    end

endmodule
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

